/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年11月29日 15:28:40
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.core.code.PermitType;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.service.sys.dto.PermitTypeDto;
import com.pactera.jep.sys.model.DashWidget;
import com.pactera.jep.sys.model.DashWidgetDak;
import com.pactera.jep.sys.service.DashWidgetDakService;
import com.pactera.jep.sys.service.DashWidgetService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.controller.CrudController;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组件授权控制器
 *
 * @author zy
 * @time 2017年12月18日 上午9:25:56
 * @company 文思海辉技术有限公司
 */
@RestController
@RequestMapping("/dashWidgetDak")
public class DashWidgetDakRestController extends CrudController<DashWidgetDak> {
    private static Logger logger = LoggerFactory.getLogger(DashWidgetDakRestController.class);

    @Autowired
    private DashWidgetDakService dashWidgetDakService;

    @Autowired
    private DashWidgetService dashWidgetService;

    /**
     * 为列表页面做数据准备
     */
    @ApiOperation("为列表页面做数据准备")
    @Override
    protected void prepareQuery(Page<DashWidgetDak> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {

        dashWidgetDakService.query(page, filters, orders);
    }

    /**
     * 保存
     *
     * @param dashWidgetDak
     * @return
     */
    @ApiOperation("保存")
    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody DashWidgetDak dashWidgetDak) {

        Map<String, Object> tips = new HashMap<String, Object>();

        Boolean isExist = dashWidgetDakService.exists(dashWidgetDak.getDakId());
        try {
            if (isExist) {
                dashWidgetDakService.update(dashWidgetDak);
            } else {
                dashWidgetDakService.insert(dashWidgetDak);
            }

            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    /**
     * 获取数据权限数据
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("获取数据权限数据")
    @GetMapping(value = "/permitTypes")
    public Map<String, Object> permitTypes() throws Exception {
        Map<String, Object> data = new HashMap<>();
        List<PermitTypeDto> items = new ArrayList<PermitTypeDto>();

        for (PermitType permitType : PermitType.values()) {
            items.add(new PermitTypeDto(permitType.getName(), permitType.getCode(), null));
        }
        data.put("rows", items);
        return data;
    }

    /**
     * 获取选中的组件数据
     *
     * @param dashWidgetDak
     * @return
     * @throws Exception
     */
    @ApiOperation("获取选中的组件数据")
    @PostMapping(value = "/getWidgetData")
    public List<DashWidget> getWidgetData(@RequestBody  DashWidgetDak dashWidgetDak) throws Exception {
        List<DashWidget> items = new ArrayList<DashWidget>();

        List<DashWidgetDak> resultList = dashWidgetDakService.select(new Filter.Builder()
                .eq("dakUsingCode", dashWidgetDak.getDakUsingCode())
                .eq("dakMapType", dashWidgetDak.getDakMapType())
                .build());

        for (DashWidgetDak result : resultList) {
            items.add(dashWidgetService.get(result.getWidgetCode()));
        }

        return items;
    }

    /**
     * 获取选中的数据权限数据
     *
     * @param dashWidgetDak
     * @return
     * @throws Exception
     */
    @ApiOperation("获取选中的数据权限数据")
    @PostMapping(value = "/getPermitData")
    public Map<String, Object> getPermitData(@RequestBody DashWidgetDak dashWidgetDak) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("dakId", null);
        List<PermitTypeDto> items = new ArrayList<PermitTypeDto>();

        List<DashWidgetDak> resultList = dashWidgetDakService.select(new Filter.Builder()
                .eq("widgetCode", dashWidgetDak.getWidgetCode())
                .eq("dakUsingCode", dashWidgetDak.getDakUsingCode())
                .eq("dakMapType", dashWidgetDak.getDakMapType())
                .build());

        for (DashWidgetDak result : resultList) {
            for (PermitType permitType : PermitType.values()) {
                if (result.getDakCode().equals(permitType.getCode())) {
                    data.put("dakId", result.getDakId());
                    items.add(new PermitTypeDto(permitType.getName(), permitType.getCode(), true));
                }
            }
        }
        data.put("permitType", items);
        return data;
    }
}